package com.example.testspringboot.controller;

import com.example.testspringboot.bean.Cate1;
import com.example.testspringboot.bean.Student;
import com.example.testspringboot.serve.CateServiceInf;
import io.micrometer.core.instrument.Counter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
public class TestController {
    @Autowired
    private Counter counter1;
    @Autowired
    private Student student;
    @Autowired
    private CateServiceInf cateService;

    @GetMapping("test")
    public String test(){

        cateService.test();

        counter1.increment();

        List<Cate1> cate1s = new ArrayList<>();
        Cate1 cate1 = new Cate1();
        cate1.setId(1);
        cate1.setName("测试");
        cate1.setValue("测试value");
        cate1s.add(cate1);

        Cate1 cate2 = new Cate1();
        cate2.setId(2);
        cate2.setName("开发");
        cate2.setValue("测试value2");
        cate1s.add(cate2);

        Cate1 cate3 = new Cate1();
        cate3.setId(3);
        cate3.setName("测试");
        cate3.setValue("测试value2");
        cate1s.add(cate3);

        Map<Integer, List<Cate1>> cate1Map = cate1s.stream().collect(Collectors.groupingBy(Cate1::getId));
        // 遍历cate1Map
        for (Map.Entry<Integer, List<Cate1>> entry : cate1Map.entrySet()) {
            Integer id = entry.getKey(); // 获取分组键（ID）
            List<Cate1> cate1List = entry.getValue(); // 获取对应ID的所有Cate1对象列表

            for (Cate1 cateItem : cate1List) {
                // 处理每个Cate1对象
                System.out.println("ID: " + id + ", Cate1 info: " + cateItem.toString());
            }
        }


        Map<Integer, Cate1> stringCate1Map = convertToNameMap(cate1s);
        System.out.println(stringCate1Map);
        for (Map.Entry<Integer, Cate1> entry : stringCate1Map.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue().toString());
        }
        return "hello";
    }

    public Map<Integer, Cate1> convertToNameMap(List<Cate1> categories) {
        Map<Integer, Cate1> nameMap = new HashMap<>();

        for (Cate1 category : categories) {
            nameMap.put(category.getId(), category);
        }

        return nameMap;
    }
}
